package edu.gettysburg.catapp1;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.MediaController;
import android.widget.VideoView;
import android.support.v4.app.NavUtils;

public class PlayVid extends Activity {
	private Uri uri;
	private MediaController mc;
	private VideoView vid;
	
    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     * 
     * Sets up the video from given source:
     * - retrieves path of video file from intent
     * - creates Uri from given path
     * - Creates and adds mediaController to centered VideoView
     */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.play_vid);
        
        Intent intent = getIntent();
        String message = intent.getStringExtra(getPackageName());
        
        vid = (VideoView) findViewById(R.id.videoView);
        
        File f = new File(message);
        
        uri = Uri.fromFile(f);
		mc = new MediaController(this);
		mc.setAnchorView(vid);
		vid.setMediaController(mc);

		vid.setVideoURI(uri);
		vid.setMediaController(mc);
		
		if(savedInstanceState != null) {
			vid.seekTo(savedInstanceState.getInt("pos"));
			if(savedInstanceState.getBoolean("play")) {
				vid.start();
			}
		}
		else {
			vid.start(); 
		}

        getActionBar().hide();
        
    }

    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     * 
     * Overridden method: Action bar is hidden.
     */
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.play_vid, menu);
        return true;
    }

    
    /*
     * (non-Javadoc)
     * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
     * 
     * Overridden method: Action bar is hidden.
     */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    /*
     * (non-Javadoc)
     * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
     * 
     * @param outState: state saved right before onDestroy()
     * 
     * This method saves the state of the video in case it needs to be rebuilt
     *    - ie. after orientation change
     */
    public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("pos", vid.getCurrentPosition());
		if(vid.isPlaying()) {
			boolean play = true;
			outState.putBoolean("play", play);
		}
		else {
			boolean play = false;
			outState.putBoolean("play", play);
		}
	}

}